package br.com.pfm.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import br.com.pfm.dao.SequencialDao;
import br.com.pfm.persistence.model.Sequencial;

@Repository
public class SequencialDaoImpl implements SequencialDao {
	
	@PersistenceContext(unitName="pfm")
    private EntityManager em;
	
	@Override
	public int nextSeq(String nomeParam){
		
		Query query = em.createQuery("SELECT s FROM Sequencial as s WHERE nomeParametro = :pNomeParam");
		
		query.setParameter("pNomeParam", nomeParam);
		
		Sequencial sequencial = (Sequencial) query.getSingleResult();
		
		int nextSeq = sequencial.getSeqParametro() + 1;
		
		sequencial.setSeqParametro(nextSeq);
		
		em.merge(sequencial);
		
		return nextSeq;
	}

}
